const axios = require("axios");
const http = axios.create()
// 页码
let page = 1;


// 添加请求拦截器
axios.interceptors.request.use(function (config) {
    // 对查询字符串的值进行url百分号编码
    const params = config.params
    if (typeof params === 'object' && params !== null) {
        for (let key in params) {
            let value = params[key]
            params[key] = encodeURI(value)

        }
    }
    config.params = params
    return config;
}, function (error) {
    return Promise.reject(error);
});
const get = function (url, config) {
    return http.get(url, config).then((response) => {
        const { data, status, statusText } = response
        if (status == 200 || status == 304) {
            return data;
        }
        throw new Error(statusText)
    });
}
export const getIninMovie = function () {
    // return get('/ajax/movieOnInfoList')
    page = 1;
    return get('/mock/initMovie.json')
}

export const getMoreMovies = function (movieIds) {
    // return get("/ajax/moreComingList", {
    ++page;
    return get("/mock/moreMovie.json", {

        params: {
            token: "",
            movieIds: movieIds.join(",")
        }
    }).then((data) => {
        const { coming } = data;
        const newComing = coming.map((movie) => {
            movie.id = movie.id + page;
            return movie;
        })
        return { coming: newComing }
    })
}
export const getCities = function () {
    return get("/mock/cities.json"); // 返回的是promise
}